package net.qiqbframework.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitBindingConfig {

    public static final String QUEUE_NAME = "qiqb.event.queue_";
    public static final String EXCHANGE_NAME = "qiqb.event";

    @Value("${project.name}")
    private String projectName;

    @Bean
    public Queue qiqbEventQueue() {
        return new Queue(  QUEUE_NAME+projectName);
    }

    @Bean
    public FanoutExchange qiqbEventExchange() {
        return new FanoutExchange(EXCHANGE_NAME);
    }

    @Bean
    public Binding qiqbEventBinding() {
        return BindingBuilder.bind(qiqbEventQueue()).to(qiqbEventExchange());
    }

    @Bean
    public RabbitEventChannel rabbitEventChannel(RabbitTemplate rabbitTemplate, net.qiqbframework.config.Configuration c) {
        return new RabbitEventChannel(rabbitTemplate, c.eventSerializer());
    }

    @Bean
    public RabbitEventReceiver rabbitEventReceiver(net.qiqbframework.config.Configuration c) {
        return new RabbitEventReceiver(c.eventGateway(), c.eventSerializer());
    }

}
